<?php 
/**
 * 车辆管理类
 * @Author    紫梦
 * @DateTime  2017-11-03
 * @copyright 响响租车
 * @version   1.0
 * @UpdateTime 2017-11-10
 */
namespace app\wxapp\controller;
use \think\Request;
use \think\Db;
use app\common\model\Agency;
use app\common\model\Company;
use app\common\model\CarType;
use app\common\model\Brand;
use app\common\model\Cars as CarsModel;
use app\common\model\CarAttr;
use app\common\request\ClientLai;
use app\common\request\Oss;
use app\common\controller\Execl;


class Cars extends Base
{
	/**
	 *车辆列表
	 */
	public function index($page=1,$length=10,$seach='')
	{		
		$model= new CarsModel;
		if(empty($seach)){
			$where = '1=1';
		}else{
			$where = [
				'car_sn|car_id|car_unique_id|equipment_id'=>['like','%'.$seach.'%']
				];
		}
		try {			
			$data = $model->order('car_id', 'desc')
						->where($where)
						->paginate($length,false,[
							    'type'     => 'bootstrap',
							    'var_page' => 'page',
							    'page'=>$page ,
							]);				
		} catch (\Exception $e) {
			
			return [
				'code'=>2,
				'msg'=>$e->getMessage(),
				];
		}
		return $data;
	}

	public function add()
	{
		$request = Request::instance();
		if($request->isPost()){
			$data = $request->post();
			$model = new CarsModel();			

			$validate = validate('Cars');

			if(!$validate->check($data)){
			    return [
					'code'=>2,
					'msg'=>$validate->getError(),
				];
			}
			try {
				$model->allowField(true)->save($data);
	            $id =$model->car_id;                				
			} catch (Exception $e) {
				return [
					'code'=>3,
					'msg'=>$e->getMessage(),
				];
			}
            if($id){
	            return [
	                'code'=>1,
	                'id' => $id,
	                'msg'=>'添加成功'
	                ];
            
	        }else{
	            return [
	                'code'=>2,
	                'msg'=>'添加失败,请稍后再试'
	                ];
	        }
		}else{
			$data['brand'] = Brand::field('id,name')->select();
			$data['holder'] = Company::field('id,name')->select();
			$data['type'] = CarType::field('id,name')->select();
			$data['agency'] = Agency::field('id,name')->select();
			
			return $data;  

		}
	}

	/**
	 * 车辆详情页
	 */
	public function details($id=1)
	{
		
		try {			
			$data = CarsModel::get($id);			
		} catch (\Exception $e) {
			
			return [
				'code'=>2,
				'msg'=>$e->getMessage(),
				];
		}
		$data ->car_file;
		$data['holder_name'] = $data->holder_name;
		$data['agency_name'] = $data->Agency_name;
		$data['brand_name'] = $data->brand_name;
		return $data;
	}

	// 添加附件
    public function add_file()
    {	
    	$request = Request::instance();
    	$id = $request->post('id');
    	$type = $request->post('type');
        $file = $request->file('uploadfile');
        if($file){
            $oss = new Oss();
           
            $url = $oss->uploadOss($file);                    
            if($url){
                try {
                    Db::name('car_file')->insert(['car_id'=>$id,'file_type'=>$type,'file_url'=>$url]);
                    
                } catch (Exception $e) {
                    return [
                        'code'=>2,
                        'msg'=>$e->getMessage(),
                        ];
                }
            }
            
        } 
        return [
            'code'=>1,
            'msg'=>'上传成功'
            ];
    }

	/**
	 * 删除车辆
	 */
	public function delete($id)
	{
		$msg['code'] = 1;
		$msg['msg'] = '删除成功';
		$data = CarsModel::get($id);
		if($data['band_status'] == 1){
			$msg['code'] = 3; 
			$msg['msg'] = '删除失败,请解除绑定';
			return $msg;
		}
		try {
			CarsModel::destroy($id);
		} catch (Exception $e) {
			return [
				'code'=>2,
				'msg'=>$e->getMessage()
				];
		}
		
		return $msg;
	}


	/**
	 * 绑定车辆
	 */
	public function bind_car($id)
	{
		$config = config('blt_lai');
		$obj = new ClientLai($config['user_name'],$config['password']);
		$model = CarsModel::get($id);
		$data['supplier_no'] = $model['supplier_no'];
		$data['car_unique_id'] = $model['car_unique_id'];
		$data['equipment_id'] = $model['equipment_id'];
		$data['bluetooth_id'] = ltrim($model['equipment_id'],0);
		$data['license_plate'] = $model['car_sn'];
		
		$res = $obj->request_post(rtrim($config['url'],'/').'/bind_car',$data);

		$arr = json_decode($res,true);


		if($arr['result']==0){			
			try{
				$model->band_status = 1;
				$model->save();
			    
			}catch(\Exception $e){
				$arr['result'] = 1;
			    $arr['msg'] = '绑定成功,但是数据库状态更新失败';
			}

		}
		return $arr;
		
	}

	/**
	 * 解绑车辆
	 */
	public function unbind_car($id)
	{

		$config = config('blt_lai');
		$obj = new ClientLai($config['user_name'],$config['password']);
		$model = CarsModel::get($id);
		$data['supplier_no'] = $model['supplier_no'];
		$data['car_unique_id'] = $model['car_unique_id'];
		
		$res = $obj->request_post(rtrim($config['url'],'/').'/unbind_car',$data);

		$arr = json_decode($res,true);


		if($arr['result']==0){			
			try{
				$model->band_status = 0;
				$model->save();
			    
			}catch(\Exception $e){
				$arr['result'] = 1;
			    $arr['msg'] = '解绑成功,但是数据库状态更新失败';
			}

		}
		return $arr;
	}





}